/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agilelang.editor.listener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
//import org.agilelang.db.generator.GMySql;
//import org.agilelang.fgl.parser.Parser;
//import org.agilelang.fgl.scanner.Scanner;
//import org.agilelang.object.Project;

/**
 *
 * @author RODERICK
 */
public class Compile {

    public static void scanner_parser(String path_source) {
        /*Scanner scaner = new Scanner();
        System.out.println("Scaner : " + path_source + "\n");

        List<File> files = addFiles(null, new File(path_source + File.separator + "src"));
        System.out.println(files);
        for (int i = 0; i < files.size(); i++) {
            File lenguaje = files.get(i);
            System.out.println("file " + lenguaje.getAbsolutePath() + "\n");
            if (!scaner.process(lenguaje.getAbsolutePath())) {
                //System.err.println(scaner.getError());
                System.out.println("ERROR : " + scaner.getError() + "\n");
                return;
            }
        }


        System.out.println("Parser..." + "\n");

        Parser parser = new Parser();
        parser.process(scaner.getLines());
        try {
            //path_xml = (new File(path_source)).getParentFile().getAbsolutePath() + "/meta.xml";
            String path_xml = (new File(path_source)).getAbsolutePath() + File.separator + "out" + File.separator + "/meta.xml";
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path_xml)));
            System.out.println(path_xml);
            pw.print(parser.getXml());
            pw.close();
            System.out.println("XML Generate : " + path_xml + "\n");
        } catch (IOException e) {
            System.out.println("ERROR : " + e.toString() + "\n");
        }*/
    }

    public static List<File> addFiles(List<File> files, File dir) {
        if (files == null) {
            files = new LinkedList<File>();
        }

        if (!dir.isDirectory()) {
            files.add(dir);
            return files;
        }

        for (File file : dir.listFiles()) {
            if (file.getName().endsWith(".agile")) {
                addFiles(files, file);
            }
        }
        return files;
    }

    public static void generatesql(String path_file_xml) {

/*
        //leer xml
        InputStream is = null;
        Reader reader = null;
        try {
            is = new FileInputStream(path_file_xml);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //----------------------------------------------------------------------
        //lleno la clase
        Project project = null;
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(Project.class);
            Unmarshaller u = jc.createUnmarshaller();
            project = (Project) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.out.println("mal : " + ex.toString());
        }
        //------------------------------------------------------------------


        System.out.println(project.application.name);

        GMySql db = new GMySql();
        db.setDB(project.application.source);
        db.setConectionParams(project.application.database, project.application.login, project.application.password);
        db.setModelList(project.models);
        //db.setScaffoldList(project.scaffolds);
        db.writeFileSql("frame.getPathProject()" + File.separator + "out");
        //db.createDB();
        //System.out.println(db.getFileSql());
        */
    }
}
